package com.otitan.la.forest.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.otitan.la.forest.system.dto.DicTypeDTO;
import com.otitan.la.forest.system.entity.DicTypeEntity;
import com.otitan.la.forest.system.service.DicTypeService;
import com.otitan.webapp.framework.basepro.model.DataResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 字典类型表
 *
 * @author jiacs
 * @email xxx
 * @date 2020-09-17 12:53:59
 */
@Slf4j
@Api(tags = "字典类型表API")
@RestController
@RequestMapping("/dic/type")
public class DicTypeController {

    @Autowired
    private DicTypeService dicTypeService;


    @ApiOperation(value = "新增")
    @PostMapping
    public DataResult add(@Valid @RequestBody DicTypeEntity dicType, BindingResult bindingResult) {
        return dicTypeService.add(dicType, bindingResult);
    }

    @ApiOperation(value = "批量删除")
    @DeleteMapping("/batchDelete")
    public DataResult batchDelete(@RequestBody @ApiParam(value = "ID集合") List<String> ids) {
        return dicTypeService.batchDelete(ids);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/delete/{id}")
    public DataResult delete(@PathVariable("id") String id) {
        List<String> ids = new ArrayList<String>() {{
            add(id);
        }};
        return dicTypeService.batchDelete(ids);
    }

    @ApiOperation(value = "更新")
    @PutMapping
    public DataResult editor(@Valid @RequestBody DicTypeEntity dicType, BindingResult bindingResult) {
        return dicTypeService.editor(dicType, bindingResult);
    }

    @ApiOperation(value = "列表查询（分页）")
    @PostMapping("/findPage/{page}/{size}")
    public DataResult<Page<DicTypeEntity>> findPageData(@PathVariable("page") long page,
                                                        @PathVariable("size") long size,
                                                        @RequestBody DicTypeDTO dicType) {
        return dicTypeService.findPageData(page, size, dicType);
    }

    @ApiOperation(value = "根据ID查询")
    @GetMapping("/findOne/{id}")
    public DataResult<DicTypeEntity> findOne(@PathVariable("id") String id) {
        return dicTypeService.findOne(id);
    }

    @ApiOperation(value = "获取字典类型（下拉框）")
    @GetMapping("/getDicTypeOptions")
    public DataResult<Page<DicTypeEntity>> getDicTypeOptions() {
        return dicTypeService.getDicTypeOptions();
    }
}
